<template>
  <div class="shopping">
    <div class="shopoing-nav" v-show="shopingcheck">
      <van-nav-bar title="购物车" left-arrow @click-left="rutern" />
    </div>
    <!-- 购物车空 -->
    <div class="top-top" v-show="shopingcheck">
      <div class="shopping-top">
        <div class="kong">
          <div class="top-kong">
            <van-empty
              class="custom-image"
              image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
              description="购物车是空的，去挑选几件商品吧"
            />
          </div>
          <div class="top-tiaozhuan" @click="JumpCategory">去逛逛</div>
        </div>
      </div>

      <div class="shopping-title">———为你推举———</div>
      <div class="shopping-footer">
        <div
          class="footer-itme"
          v-for="(item, index) in Shingin.data"
          :key="index"
          @click="productDetails(item.id)"
        >
          <div class="item-img">
            <img class="auto-img" :src="item.img" alt="" />
          </div>
          <div class="item-content">{{ item.title }}</div>
          <div class="item-price">&yen;{{ item.price_sale }}</div>
        </div>
      </div>
    </div>

    <!-- 商品内容 -->
    <div class="pro-conetnt" v-show="!shopingcheck">
      <div class="top-back">
        <span class="spanreturn" @click="clickReturn">返回</span>
        <h1 class="hh1">购物车</h1>
        <span class="span" @click="clickTab">{{ istitle }}</span>
      </div>

      <div class="midden-center">
        <div class="top-name">
          <div class="name">360商城自营</div>
          <div class="lingjun">匠心品质</div>
        </div>
        <div class="center-box">
          <div class="content" v-for="(item, index) in totalData" :key="index">
            <div class="livingDetails">
              <!-- 购物车商品 -->
              <div class="living-item">
                <div class="item-top">
                  <div class="top-right">
                    <div class="top-right-img">
                      <img
                        class="auto-img"
                        src="	https://img2.baidu.com/it/u=46579649,821546474&fm=253&fmt=auto&app=138&f=PNG?w=500&h=500"
                        alt=""
                      />
                    </div>
                    <div class="top-right-content">360官方自营</div>
                    <div class="top-right-right">领劵</div>
                  </div>
                </div>

                <div class="itme-right">
                  <div class="right-input">
                    <!-- 单选 -->
                    <input
                      name="checkbox"
                      value="Item 1"
                      type="checkbox"
                      class="tui-checkbox"
                      :checked="item.chebox"
                      @click="btnRoder(item.countID)"
                    />
                  </div>
                  <div class="right-img">
                    <img class="auto-img" :src="item.img" alt="" />
                  </div>

                  <div class="right-box">
                    <div class="right-name">{{ item.name }}</div>
                    <div class="ClassName">分类:{{ item.type }}</div>
                    <div class="right-price">
                      <div class="price">&yen;{{ item.currentPrice }}</div>
                      <div class="price-right">
                        <van-stepper
                          v-model.number="item.iscount"
                          @plus="appcountToTal(item.countID)"
                          @minus="reducecountToTal(item.countID)"
                        />
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <!-- <LivingDetails :pro="item" @click="addFun(index)"></LivingDetails> -->
          </div>
        </div>
      </div>
      <div class="footer">
        <div class="dingdan" v-if="!isindex">
          <van-submit-bar
            :price="totalPriceData"
            button-text="去结算"
            @submit="clickOrder"
          >
            <template #tip>
              <span class="countSpan">已选择 ( {{ totalCount }} ) 件</span>
            </template>
            <!-- 全选 -->
            <van-checkbox v-model="checked" @click="btnFun">全选</van-checkbox>
          </van-submit-bar>
        </div>

        <div class="deleft" v-if="isindex">
          <van-submit-bar button-text="移入收藏">
            <van-checkbox v-model="checked" @click="btnFun">全选</van-checkbox>
            <template>
              <div>
                <div class="remove" @click="removeChecked()">删除选中</div>
              </div>
            </template>
          </van-submit-bar>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Dialog } from "vant";
import LivingDetails from "../components/LivingDetails.vue";
import Shingin from "../../public/json/Shingin.json";
export default {
  name: "Shopping",
  components: {
    LivingDetails,
  },
  data() {
    return {
      Shingin,
      //购物车是否含有
      shopingcheck: true,
      //全选
      checked: false,
      //加入购物车总数据
      totalData: "",
      //编辑状态
      isindex: false,
      istitle: "",
      // 总价格
      totalPriceData: 0,
      //结算数量
      totalCount: 0,
      //判断是否登录
      signin:''
    };
  },
  created() {
    //获取本地加入购物车数据
    let aaa = JSON.parse(localStorage.getItem("appCount"));
    this.totalData = aaa;
    console.log("aaa=>", this.totalData);

    this.istitle = "编辑";

    // 调用总价格
    this.totalPrice();

    this.btnFun();

    //是否为空购物车
    this.panduanFunt();
    //判断是否登录
    this.clickbendiData();
  },
  computed: {},
  methods: {
    //拿到本地存储的密码账号
    clickbendiData() {
      let bendiData = JSON.parse(localStorage.getItem("SigInID"));
      this.signin = bendiData;
      console.log("这个是点击登录", this.signin);
      if (this.signin == null) {
        this.$router.push({ name: "SignIn" });
      }
    },
    //购物车为空,返回上一页
    rutern() {
      this.$router.back();
    },
    //跳转
    JumpCategory() {
      this.$router.push({ name: "Category" });
    },
    // 这个是查看商品详情
    productDetails(itemID) {
      console.log("itemID=>", itemID);
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        loadingType: "spinner",
      });
      setTimeout(() => {
        this.$router.push({ name: "Details", params: { itemID } });
      }, 1000);
    },

    //判断购物车是否为空，为空显示空购物车界面
    panduanFunt() {
      if (this.totalData.length > 0) {
        this.shopingcheck = false;
      } else {
        this.shopingcheck = true;
      }
    },

    //结算页面
    clickOrder() {
      let aaa = [];

      this.totalData.filter((item) => {
        if (item.chebox) {
          aaa.push(item);
        } else {
          aaa;
        }
      });

      console.log("aaa=>", aaa);
      //存储到本地
      window.localStorage.setItem("DingDanChebox", JSON.stringify(aaa));

      if (aaa.length == 0) {
        this.$toast("请选择商品");
      } else {
        // 判断是否有选中商品
        this.$toast.loading({
          message: "加载中...",
          forbidClick: true,
          loadingType: "spinner",
        });
        setTimeout(() => {
          this.$router.push({ name: "Dingdan" });
        }, 1000);
      }
      console.log("alslss");
    },

    //删除选中
    removeChecked() {
      // 提示组件
      Dialog.confirm({
        message:
          "主人，是奴家哪里做的不好，要删除奴家，给奴家个机会嘛主人！！！ ",
        confirmButtonText: "狠心删除",
        cancelButtonText: "给她机会",
      })
        .then(() => {
          //过滤，返回未选中得
          let cheboxData = this.totalData.filter((item) => {
            return !item.chebox;
          });
          console.log(cheboxData);

          this.totalData = cheboxData;
          if (this.totalData.length == 0) {
            this.$router.push({ name: "Shopping" });
          }

          //把通过过滤得存储到本地
          window.localStorage.setItem(
            "appCount",
            JSON.stringify(this.totalData)
          );
          this.btnFun();

          // 调用总价格
          this.totalPrice();
        })
        .catch(() => {});

      //  储存到本地

      // console.log(JSON.parse(localStorage.getItem("appCount")));
    },

    // 全选
    btnFun() {
      this.totalData.forEach((item) => {
        item.chebox = this.checked;
        if (item.chebox) {
          this.totalCount += item.iscount;
        } else {
          this.totalCount = 0;
        }
      });

      // 调用总价格
      this.totalPrice();
      // console.log("this.totalData=>",this.totalData);
    },

    // 单选
    btnRoder(id) {
      let DataID = this.totalData.findIndex((item) => {
        return item.countID == id;
      });
      console.log("id=>", id);
      console.log("DataID=>", DataID);
      this.totalData[DataID].chebox = !this.totalData[DataID].chebox;
      // 高亮数量
      if (!this.totalData[DataID].chebox) {
        this.totalCount -= this.totalData[DataID].iscount;
      } else {
        this.totalCount += this.totalData[DataID].iscount;
      }
      // 是要有一个不通过，就假
      this.checked = this.totalData.every((item) => {
        return item.chebox;
      });

      // 调用总价格
      this.totalPrice();
    },

    // 总价格
    totalPrice() {
      let total = 0;
      this.totalData.forEach((item) => {
        // console.log("item=>",item);

        if (item.chebox) {
          total += item.iscount * item.currentPrice * 100;
        } else {
          this.totalPriceData = 0;
        }
      });
      console.log("item=>", this.totalData);

      this.totalPriceData = total;
    },

    //点击添加
    appcountToTal(id) {
      // 找到一个符合条件的就不在向下寻找
      let DataID = this.totalData.findIndex((item) => {
        return item.countID == id;
      });
      this.totalData[DataID].iscount++;
      // console.log("DataID=>",DataID);

      //调用总价格
      this.totalPrice();
      //存储到本地
      window.localStorage.setItem("appCount", JSON.stringify(this.totalData));
    },

    //点击减少
    reducecountToTal(id) {
      // 找到一个符合条件的就不在向下寻找
      let DataID = this.totalData.findIndex((item) => {
        return item.countID == id;
      });
      this.totalData[DataID].iscount--;
      // console.log("DataID=>",DataID);

      //调用总价格
      this.totalPrice();

      //存储到本地
      window.localStorage.setItem("appCount", JSON.stringify(this.totalData));
    },

    //返回上一级
    clickReturn() {
      this.$router.back();
    },
    clickTan() {
      Dialog.alert({
        title: "优惠卷",
        message: "暂无优惠券",
      }).then(() => {
        // on close
      });
    },

    clickTab() {
      this.isindex = !this.isindex;
      if (this.isindex == true) {
        this.istitle = "完成";
      } else {
        this.istitle = "编辑";
      }
    },

    //全选
    clicktotal() {
      this.ischecked = !this.ischecked;

      console.log("this.checked=>", this.ischecked);
    },
    // 立即购买
    clickGoumai() {
      this.$router.push({ name: "Dingdan" });
    },

    // 提交订单
  },
};
</script>

<style lang="scss" scoped>
.countSpan {
  position: absolute;
  left: 70px;
  top: 6px;
  font-size: 16px;
  font-weight: bold;
  color: #ff001e;
  padding: 8px;
  transform: translate(-50%);
}

.van-submit-bar__tip {
  background-color: white;
  height: 30px;
}

.price-right {
  width: 100px;
  margin-left: auto;
}
.price {
  font-size: 16px;
  color: red;
  font-weight: bold;
}
.right-box {
  position: relative;
}
.right-price {
  display: flex;
  position: absolute;
  bottom: 0;
  width: 200px;
}
.ClassName {
  margin-top: 10px;
  font-size: 14px;
  color: #999;
}

.right-name {
  margin-top: 5px;
  font-size: 14px;
  width: 200px;
  max-height: 40px;
}
.right-img {
  height: 100px;
  width: 100px;
  margin-top: 5px;
  margin-left: 10px;
  border: 1px solid rgb(213, 212, 212);
  margin-right: 10px;
}
.right-input {
  margin-top: 42px;
}
.top-right-right {
  margin-left: auto;
  font-size: 16px;
  line-height: 27px;
}
.item-top {
  display: flex;
  width: 100%;
}
.top-right {
  height: 30px;
  display: flex;
  width: 100%;
  // margin-left: 20px;
  // justify-content: center;
  .top-right-content {
    font-size: 16px;
    margin-left: 10px;
    font-weight: bold;
    line-height: 27px;
  }
}
.top-right-img {
  width: 20px;
  height: 20px;
  margin-top: 3px;
}
.living-item {
  border-radius: 10px;
}
.livingDetails {
  padding: 10px;
}

.itme-right {
  width: 100%;
  height: 110px;
  margin-top: 10px;
  border: 1px solid white;
  display: flex;
}

//设置选中背景
.tui-checkbox:checked {
  background: #1673ff;
}
//设置复选框样式
.tui-checkbox {
  width: 23px;
  height: 23px;
  background-color: #ffffff;
  border: solid 2px #cacaca;
  -webkit-border-radius: 50%;
  border-radius: 50%;
  font-size: 0.8rem;
  margin: 0;
  padding: 0;
  position: relative;
  display: inline-block;
  vertical-align: top;
  cursor: default;
  -webkit-appearance: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-transition: background-color ease 0.1s;
  transition: background-color ease 0.1s;
}
//设置伪类，即显现的对勾样式
.tui-checkbox:checked::after {
  content: "";
  top: 3px;
  left: 3px;
  position: absolute;
  background: transparent;
  border: #fff solid 2px;
  border-top: none;
  border-right: none;
  height: 6px;
  width: 10px;
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}
.living-item {
  background-color: white;
}

.item {
  margin-bottom: 10px;
}
.remove {
  width: 110px;
  height: 40px;
  line-height: 40px;
  border: 1px solid #e4393c;
  border-radius: 20px;
  text-align: center;
  font-size: 16px;
  color: #e4393c;
  margin-right: 5px;
  margin-left: 60px;
}
.spanreturn {
  color: white;
  left: 20px;
  top: 35px;
  position: absolute;
}
.lingjun {
  margin-left: auto;
  margin-right: 10px;
  line-height: 35px;
  font-size: 16px;
  font-weight: bold;
}
.name {
  margin-left: 10px;
  line-height: 35px;
  font-size: 16px;
  font-weight: bold;
}
.top-name {
  height: 35px;
  display: flex;
  border-bottom: 1px solid rgb(211, 211, 211);
  background-color: rgb(255, 255, 255);
  border-radius: 10px;
}
.midden-center {
  width: 100%;
  margin: 0 auto;
  position: relative;
  top: -90px;

  .content {
    margin-top: 10px;
    border-radius: 10px;
    margin: 10px 5px 0 5px;
    border-radius: 10px;
    background-color: white;
    border: 1px solid rgb(211, 211, 211);
  }
}
.pro-conetnt {
  background-color: #f5f5f5;
  position: fixed;
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
.span {
  color: white;
  position: absolute;
  right: 20px;
  top: 35px;
}
.hh1 {
  position: absolute;
  margin: 10px auto;
  color: white;
  left: 50%;
  top: 10px;
  transform: translate(-50%);
}
.top-back {
  position: relative;
  height: 160px;
  width: 100%;
  background-color: #f24d60;
}
.top-tiaozhuan {
  height: 30px;
  width: 100px;
  border: 2px solid #e4393c;
  color: #e4393c;
  text-align: center;
  line-height: 30px;
  margin: 0 auto;
}
.shopping-top {
  // width: 100%;
  background-color: white;
  padding-bottom: 60px;
}
.shopping-title {
  width: 100%;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-size: 18px;
  box-sizing: border-box;
  font-weight: bold;
}
.item-price {
  font-size: 16px;
  margin-top: 5px;
  color: #e4393c;
}
.item-content {
  font-size: 16px;
  font-weight: bold;
}
.footer-itme {
  width: calc(50% - 10px + 9px);
  padding: 10px;
  box-sizing: border-box;
  background-color: white;
  margin-right: 10px;
  &:nth-child(2n) {
    margin-right: 0;
  }
}

.shopping-footer {
  display: flex;
  width: 100%;
  padding: 10px;
  box-sizing: border-box;
}

.shopping {
  background-color: #f6f6f7;
  width: 100%;
}
</style>